package com.kanqiubao.kq.view.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.horizontalgridpage.HorizontalGridPage;
import com.bigkoo.horizontalgridpage.PageBuilder;
import com.bigkoo.horizontalgridpage.PageCallBack;
import com.bigkoo.horizontalgridpage.PageGridAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.kanqiubao.kq.R;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 礼物列表
 */
public class LiveGiftPopupView extends BottomPopupView implements PageCallBack {
    private HorizontalGridPage mPageView;
    private PageGridAdapter mLiveGiftListAdapter;

    public LiveGiftPopupView(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.live_detail_lw_page_layout;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        mPageView = findViewById(R.id.pageView);

    }

    private PageBuilder builder() {
        // 构建参数
        PageBuilder pageBuilder = new PageBuilder.Builder()
                .setGrid(2, 4)//设置网格
                .setPageMargin(0)//页面边距
                .setIndicatorMargins(5, 5, 5, 5)//设置指示器间隔
                .setIndicatorSize(10)//设置指示器大小
                .setIndicatorRes(android.R.drawable.presence_invisible,
                        android.R.drawable.presence_online)//设置指示器图片资源
                .setIndicatorGravity(Gravity.CENTER)//设置指示器位置
                .setSwipePercent(50)//设置翻页滑动距离百分比（1-100）
                .setShowIndicator(true)//设置显示指示器
                .build();
        return pageBuilder;
    }

    private void setData() {
        mPageView.init(builder());
        mLiveGiftListAdapter = new PageGridAdapter<String>(this);
        mLiveGiftListAdapter.init(builder());
        mPageView.setAdapter(mLiveGiftListAdapter);
        ArrayList<String> ls = new ArrayList<>();
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        mLiveGiftListAdapter.setData(ls);
    }

    @Override
    protected void onShow() {
        super.onShow();
        setData();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getWindowHeight(getContext()) * .85f);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new pageViewHodler(LayoutInflater.from(getContext()).inflate(R.layout.live_lw_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        if (i == mCurrentPosition) {
            ((pageViewHodler) viewHolder).root.setBackgroundResource(R.drawable.live_lw_select_bg);
        } else {
            ((pageViewHodler) viewHolder).root.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.tran));
        }

    }

    private int mCurrentPosition;

    @Override
    public void onItemClickListener(View view, int position) {
        mCurrentPosition = position;
        mLiveGiftListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemLongClickListener(View view, int position) {

    }

    public static class pageViewHodler extends RecyclerView.ViewHolder {
        public SimpleDraweeView mSimpleDraweeView;
        public TextView txt1;
        public TextView txt2;
        public LinearLayout root;

        public pageViewHodler(@NonNull View itemView) {
            super(itemView);
            mSimpleDraweeView = itemView.findViewById(R.id.face);
            txt1 = itemView.findViewById(R.id.txt1);
            txt2 = itemView.findViewById(R.id.txt2);
            root = itemView.findViewById(R.id.img_root_id);
        }
    }
}